﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

namespace ISUM_V2
{

    class SerchEnableMorseCode
    {
        private ArrayList MorseImage, SetImage, CustomButton, CurrentButton, MorseCode;

        private MorseCodeFileModule mcfm;

        private int  SaveCount = 0;

        private bool IsCanSave = true;
        

        public SerchEnableMorseCode()
        {
            MorseCode = new ArrayList();
        }

        public void AddMorseImage(ArrayList tempArr) // 
        {
            MorseImage = tempArr;
        }

        public void AddSetImage(ArrayList tempArr) // 변경된 모스 코드 Source가 저장된 곳
        {
            SetImage = tempArr;
        }

        public void AddButton(ArrayList customButton) // 
        {
            CustomButton = customButton;
        }

        public void AddCurrentButton(ArrayList currentButton)
        {
            CurrentButton = currentButton;
        }

        public void SearchEnableMorseCodeImage()
        {
            bool[] isContain = GetEnableFalse();
            Button tempButton;

            for (int i = 0; i < MorseImage.Count; i++)
            {

                tempButton = (Button)CustomButton[i];
                tempButton.TabIndex = 25;
            }
           
            for (int i = 0; i < SetImage.Count; i++)
            {
                isContain[MorseImage.IndexOf((string)SetImage[i])] = true;
            }
            
            ShowEnableResult(isContain);
        }

        public void SearchOverlapMorseCodeImage()
        {
            string strTemp;

            IsCanSave = true;

            foreach (Button tempButton in CurrentButton)
            {
                tempButton.TabIndex = 25;
            }

            for (int i = 0; i < SetImage.Count; i++)
            {
                bool[] isContain = GetOverlapFalse();
                SaveCount = 0;
                strTemp = (string)SetImage[i];
            
                for (int j = 0; j < SetImage.Count; j++)
                {
                    if (strTemp.Equals((string)SetImage[j]))
                    {
                        isContain[j] = true;
                        SaveCount++;
                    }
                }
                if (SaveCount != 1)
                {
                    ShowOverlapResult(isContain);
                }

            }
        }

        private void ShowEnableResult(bool[] isContain)
        {
            Button tempButton;

            for (int i = 0; i < isContain.Length; i++)
            {
                if (!isContain[i])
                {
                    tempButton = (Button)CustomButton[i];
                    tempButton.TabIndex = 50;
                }
            }
        }

        private void ShowOverlapResult(bool[] isContain)
        {
            Button tempButton;

            IsCanSave = false;

            for (int i = 0; i < isContain.Length; i++)
            {
                if (isContain[i])
                {
                    tempButton = (Button)CurrentButton[i];
                    tempButton.TabIndex = 50;
                }
            }
        }

        private bool[] GetEnableFalse()
        {
            bool[] IsContain = new bool[MorseImage.Count];
            for (int i = 0; i < MorseImage.Count; i++)
            {
                IsContain[i] = false;
            }

            return IsContain;
        }

        private bool[] GetOverlapFalse()
        {
            bool[] IsContain = new bool[SetImage.Count];

            for (int i = 0; i < SetImage.Count; i++)
            {
                IsContain[i] = false;
            }

            return IsContain;
        }

        private ArrayList GetTransformByMorseCode()
        {
            MorseCode.Clear();
            string str;
            for (int i = 0; i < SetImage.Count; i++)
            {
                str = (string)SetImage[i];
                str = str.Replace("pack://application:,,,/ISUM_V2;component/Image/dashdot/_", "");
                str = str.ToLower();
                str = str.Replace(".png", "");
                str = str.Replace("dot", ".");
                str = str.Replace("dash", "-");
                Console.WriteLine(str);
                MorseCode.Add(str);
            }

            return MorseCode;
        }

        public void Save(string fileName, bool isMouse)
        {
            
            if (IsCanSave)
            {
                mcfm = MorseCodeFileModule.getInstance();
                if (!isMouse)
                {
                    //SrzKeyboard srzKeyboard = new SrzKeyboard();
                    SrzKeyboard srzKeyboard = (SrzKeyboard)mcfm.readFile(fileName);
                    srzKeyboard.ArrKeyBoardMorseCode = GetTransformByMorseCode(); // 모스부호 ".-.--"
                    srzKeyboard.ArrKeyBoardCustomMorseCodeSrc = SetImage; // Image.Sourece.toString() 이 들어가있는 것
                    //srzKeyboard.IniArrKeyBoardMorseCodeSrcKorean = SetImage; // Image.Sourece.toString() 이 들어가있는 것

                    mcfm.writeFile(fileName, srzKeyboard); 
                }
                else
                {
                    //SrzMouse srzMouse = new SrzMouse();
                    SrzMouse srzMouse = (SrzMouse)mcfm.readFile(fileName);
                    srzMouse.ArrMouseMorseCode = GetTransformByMorseCode(); // 모스부호 ".-.--"
                    srzMouse.ArrMouseCustomMorseCodeSrc = SetImage; // Image.Sourece.toString() 이 들어가있는 것
                  //  srzMouse.InitArrMouseMorseCodeSrc = SetImage; // Image.Sourece.toString() 이 들어가있는 것
                    
                    mcfm.writeFile(fileName, srzMouse); 
                }
                // 먼저 불러와야 한다.
                
            }
            else
            {
				ErrorMessageBox messageBox = new ErrorMessageBox("저장할 수가 없습니다.");
				messageBox.ShowDialog();                
                isMouse = true;
            }
        }
       
        public void IniEnglish(string fileName)
        {

        }

        public void IniKorean(ArrayList currentImage)
        {
            mcfm = MorseCodeFileModule.getInstance(); // 파일입출력 인스턴스 불러온다

            SrzKeyboard srzKeyboard = (SrzKeyboard)mcfm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/Keyboard.data");

            ArrayList tempArr = srzKeyboard.IniArrKeyBoardMorseCodeSrcKorean;

            Image tempImage = null;

            for (int i = 0; i < currentImage.Count; i++)
            {
                tempImage = (Image)currentImage[i];
                tempImage.Source = new BitmapImage(new Uri((string)tempArr[i]));
            }
        }
        public void IniMouse(ArrayList currentImage)
        {
            mcfm = MorseCodeFileModule.getInstance(); // 파일입출력 인스턴스 불러온다

            SrzMouse srzMouse = (SrzMouse)mcfm.readFile(MainWindow.GetDoubleParent() + "/SrzMoreseCode/Mouse.data");

            ArrayList tempArr = srzMouse.InitArrMouseMorseCodeSrc;

            Image tempImage = null;

            for (int i = 0; i < currentImage.Count; i++)
            {
                tempImage = (Image)currentImage[i];
                tempImage.Source = new BitmapImage(new Uri((string)tempArr[i]));
            }
        }
    }
}
